package zhang.automobileservice;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.util.List;

import utils.ToastUtil;

public class MysettingActivity extends FarterActivity implements View.OnClickListener {

    private ImageButton finish;
    private ImageButton ib_nextOpinion;
    private ImageButton ib_nextAbout;
    private ImageButton ib_nextversion;
    private ImageButton ib_nextclear;
    private RelativeLayout rl_nextOpinion;
    private RelativeLayout rl_nextAbout;
    private RelativeLayout rl_nextclear;
    private RelativeLayout rl_nextversion;
    private ActivityManager mActivityManager;
    private int memorySize;
    private TextView tv_version;
    private TextView tv_memory;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mysetting);
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
            case R.id.rl_nextOpinion:
                Intent intent1 = new Intent(this, MyOptionActivity.class);
                startActivity(intent1);
                break;
            case R.id.rl_nextAbout:
                Intent intent2 = new Intent(this, MyAboutActivity.class);
                startActivity(intent2);
                break;
            case R.id.rl_nextversion:
                ToastUtil.show(getApplicationContext(), "您已是最新版本");
                break;
            case R.id.ib_nextclear:
                DataCleanManager.cleanExternalCache(getApplicationContext());
                DataCleanManager.cleanInternalCache(getApplicationContext());
                onResume();
                break;
        }
    }

    private void initView() {
        finish = (ImageButton) findViewById(R.id.finish);
        ib_nextOpinion = (ImageButton) findViewById(R.id.ib_nextOpinion);
        ib_nextAbout = (ImageButton) findViewById(R.id.ib_nextAbout);
        ib_nextversion = (ImageButton) findViewById(R.id.ib_nextversion);
        ib_nextclear = (ImageButton) findViewById(R.id.ib_nextclear);

        finish.setOnClickListener(this);
        ib_nextOpinion.setOnClickListener(this);
        ib_nextAbout.setOnClickListener(this);
        ib_nextversion.setOnClickListener(this);
        ib_nextclear.setOnClickListener(this);
        rl_nextOpinion = (RelativeLayout) findViewById(R.id.rl_nextOpinion);
        rl_nextOpinion.setOnClickListener(this);
        rl_nextAbout = (RelativeLayout) findViewById(R.id.rl_nextAbout);
        rl_nextAbout.setOnClickListener(this);
        rl_nextclear = (RelativeLayout) findViewById(R.id.rl_nextclear);
        rl_nextclear.setOnClickListener(this);
        rl_nextversion = (RelativeLayout) findViewById(R.id.rl_nextversion);
        rl_nextversion.setOnClickListener(this);
        tv_version = (TextView) findViewById(R.id.tv_version);
        tv_memory = (TextView) findViewById(R.id.tv_memory);
        tv_memory.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getRunningAppProcessInfo();
        tv_memory.setText(memorySize / 1000 + "M");
        tv_version.setText("v1.0");
    }

    private void getRunningAppProcessInfo() {
        mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        //获得系统里正在运行的所有进程
        List<ActivityManager.RunningAppProcessInfo> runningAppProcessesList = mActivityManager.getRunningAppProcesses();

        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcessesList) {
            // 进程ID号
            int pid = runningAppProcessInfo.pid;
            // 用户ID
            int uid = runningAppProcessInfo.uid;
            // 进程名
            String processName = runningAppProcessInfo.processName;
            // 占用的内存
            int[] pids = new int[]{pid};
            Debug.MemoryInfo[] memoryInfo = mActivityManager.getProcessMemoryInfo(pids);
            memorySize = memoryInfo[0].dalvikPrivateDirty;
        }
    }

    static class DataCleanManager {
        /**
         * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * @param context
         */
        public static void cleanInternalCache(Context context) {
            deleteFilesByDirectory(context.getCacheDir());
        }

        /**
         * * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param
         * context
         */
        public static void cleanExternalCache(Context context) {
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                deleteFilesByDirectory(context.getExternalCacheDir());
            }
        }


        /**
         * 删除方法 这里只会删除某个文件夹下的文件，如果传入的directory是个文件，将不做处理 * * @param directory
         */
        private static void deleteFilesByDirectory(File directory) {
            if (directory != null && directory.exists() && directory.isDirectory()) {
                for (File item : directory.listFiles()) {
                    item.delete();
                }
            }
        }
    }
}
